package com.lcxw.hxzbapi.controller.jubaocontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbapi.entity.pojo.Jubao;
import com.lcxw.hxzbapi.entity.requestBean.SubmitJubaoReq;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.JubaoService;
import com.lcxw.hxzbapi.service.ShortVideoService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import com.lcxw.hxzbcommon.common.StringUtil;
import javax.annotation.Resource;

import com.lcxw.hxzbcommon.entity.ErrorCode;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/jubao")
public class JubaoController {

    @Resource
    JubaoService jubaoService;

    @Resource
    ShortVideoService shortVideoService;

    @Resource
    YwpdRoomService ywpdRoomService;

    @Resource
    YwpdMemberService ywpdMemberService;

    @PostMapping("/submit")
    public Response submitJubao(@RequestBody SubmitJubaoReq submitJubaoReq) {
        Jubao jubao = new Jubao();
        BeanUtils.copyProperties(submitJubaoReq, jubao);
        jubao.setStatus("0");
        jubao.setAddTime(StringUtil.getDateTime());
        String source=jubao.getSource();
        long userId=0;
        switch (source){
            case "video":
                userId=shortVideoService.selectBySvId(Integer.valueOf(submitJubaoReq.getRoomId()+"")).getUserId();
                break;
            case "room":
                userId=ywpdRoomService.selectRoomByRoomId(submitJubaoReq.getRoomId()).getUserId();
                break;
            case "dynamic":
                userId=ywpdMemberService.selectUserId(submitJubaoReq.getRoomId());
                break;
            case "user":
                userId=submitJubaoReq.getRoomId();
                break;
        }
            jubao.setUserId(userId+"");
            jubaoService.insert(jubao);
            return Response.ok();
    }


    @PostMapping("/getDetail")
    public Response getDetail(@RequestBody JSONObject jsonObject) {
        if(null==jsonObject||StringUtil.isEmpty(jsonObject.getString("id"))){
            return Response.error(ErrorCode.INVALID_PARAM,"主键没传值!");
        }
        Jubao jubao=jubaoService.selectById(jsonObject.getInteger("id"));
        return Response.ok(jubao);
    }


    @PostMapping("/add/reason")
    public Response reason(@RequestBody Jubao jubao) {
        jubaoService.updateById(jubao);
        return Response.ok();
    }



}
